#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time   : 2022-06-06 17:51:04
# @Author : 搬砖客

import datetime
import os
from utils.process_files_utils.yaml_util import YamlUtil


def write_case(case_path, page):
    with open(case_path, 'w', encoding="utf-8") as f:
        f.write(page)


# todo 新增跳过和跳过原因的关键字
def write_testcase_file(allure_epic, allure_feature, class_title,
                        func_title, case_path, yaml_path, file_name, allure_story, whether_skip=None, skip_reason=None):
    """

        :param allure_story:
        :param file_name: 文件名称
        :param allure_epic: 项目名称
        :param allure_feature: 模块名称
        :param class_title: 类名称
        :param func_title: 函数名称
        :param case_path: case 路径
        :param yaml_path: yaml 文件路径
        :param whether_skip: 是否跳过该用例
        :param skip_reason: 跳过该用例的理由
        :return:
        """
    author = YamlUtil.read_config_yaml("project", "author")
    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    real_time_update_test_cases = YamlUtil.read_config_yaml("switch", "real_time_update_test_cases")

    page = f'''#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time   : {now}
# @Author : {author}


import allure
import pytest
from utils.process_files_utils.parameters_util import read_testcase_yaml
from utils.requests_utils.request_util import RequestUtil

real_yaml_path = '/{yaml_path}'


@allure.epic("{allure_epic}")
@allure.feature("{allure_feature}")
class Test{class_title}:
    @allure.story("{allure_story}")
    @pytest.mark.parametrize("args", read_testcase_yaml(real_yaml_path))
    def test_{func_title}(self, args):
        allure.dynamic.title("测试用例标题：%s" % (args['name']))
        if 'is_run' in args:
            if args['is_run'] is False:
                pytest.skip()
            else:
                RequestUtil().analysis_yaml(args)
        else:
            RequestUtil().analysis_yaml(args)


if __name__ == '__main__':
    pytest.main(['{file_name}', '-s', '-W', 'ignore:Module already imported:pytest.PytestWarning'])
'''
    if real_time_update_test_cases:
        write_case(case_path=case_path, page=page)
    elif real_time_update_test_cases is False:
        if not os.path.exists(case_path):
            write_case(case_path=case_path, page=page)
    else:
        raise ValueError("real_time_update_test_cases 配置不正确，只能配置 True 或者 False")
